package njci.softerware.com.myexpress2017.activity;

import android.app.Activity;
import android.os.Bundle;

import android.widget.ExpandableListView;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;

import njci.softerware.com.myexpress2017.R;
import njci.softerware.com.myexpress2017.model.ChildEntity;
import njci.softerware.com.myexpress2017.model.GroupEntity;
import njci.softerware.com.myexpress2017.model.MyAdapter;
import njci.softerware.com.myexpress2017.serviceObj.PackageInfo;

public class DetailSn extends Activity {
    private ExpandableListView expandableListView;
    private List<GroupEntity> lists;
    private MyAdapter adapter;
    private List<String[]> timeArrays;
    private List<String[][]> arryContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_sn);
        PackageInfo p = getIntent().getParcelableExtra("PackageInfo");
        initDate(p);
        initView();

    }

    private void initView() {
        lists = initList();
        adapter = new MyAdapter(this, lists);
        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        expandableListView.setAdapter(adapter);
        expandableListView.setGroupIndicator(null); // 去掉默认带的箭头
        expandableListView.setSelection(0);// 设置默认选中项
        // 遍历所有group,将所有项设置成默认展开
        int groupCount = expandableListView.getCount();
        for (int i = 0; i < groupCount; i++) {
            expandableListView.expandGroup(i);
        }

    }

    public void initDate(PackageInfo p) {
        timeArrays = new ArrayList<String[]>();
        String snTime = p.getSnTime();
        String reTime = p.getReTime();
        String diTime = p.getDisTime();
        arryContent = new ArrayList<String[][]>();
        int state = p.getStatue();
        switch (state) {
            case 0:
                String atmp[] = {reTime};
                timeArrays.add(atmp);
                String[][] a = new String[][]{{"订单已揽收", "揽收员：" + p.getReTime(), "揽收时间 | " + p.getReTime()}};
                arryContent.add(a);
                break;
            case 1:
                String bTmp[] = {diTime,reTime };
                timeArrays.add(bTmp);
                String[][] b = new String[][]{{"订单正在派送", "派送员：" + p.getDispatcher(), "派送时间 | " + p.getDisTime()},{"订单已揽收", "揽收员：" + p.getRecevier(), "揽收时间 | " + p.getReTime()}};
                arryContent.add(b);
                break;
            case 2:
                String cTmp[] = {snTime,diTime,reTime };
                timeArrays.add(cTmp);
                String[][] c = new String[][]{{"订单已签收", "签收收员：" + p.getSign(), "签收时间 | " + p.getSnTime()},{"订单正在派送", "派送员：" + p.getDispatcher(), "派送时间 | " + p.getDisTime()},{"订单已揽收", "揽收员：" + p.getRecevier(), "揽收时间 | " + p.getReTime()}};
                arryContent.add(c);
                break;
            default:
        }

    }


    private List<GroupEntity> initList() {
        String[] groupArray = timeArrays.get(0);
        String[][] childTimeArray = arryContent.get(0);
        List<GroupEntity> groupList;
        groupList = new ArrayList<GroupEntity>();
        for (int i = 0; i < groupArray.length; i++) {
            GroupEntity groupEntity = new GroupEntity(groupArray[i]);
            List<ChildEntity> childList = new ArrayList<ChildEntity>();
            for (int j = 0; j < childTimeArray[i].length; j++) {
                ChildEntity childStatusEntity = new ChildEntity(childTimeArray[i][j]);
                childList.add(childStatusEntity);
            }
            groupEntity.setChildEntities(childList);
            groupList.add(groupEntity);
        }
        return groupList;
    }

    private void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();

    }
}
